package com.example.work4jpa.Controller;

import com.example.work4jpa.Entity.Course;
import com.example.work4jpa.Service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class CourseController {
    @Autowired
    CourseService courseService;

    @GetMapping("/findall")
    public List<Course> findAll() {
        return courseService.findAll();
    }
    @GetMapping("/save")
    public Course save() {
        Course course = new Course();
        course.setId(10);
        course.setNo("C009");
        course.setName("网页设计");
        course.setTeacher("赵亮");
        course.setDepartment("计算机系");
        course.setCredit(2.0);
        return courseService.save(course);
    }
    @GetMapping("/getname/{name}")
    public Course getByName(@PathVariable String name) {
        return courseService.getByName(name);
    }

    @GetMapping("/update/{name}")
    public Course update(@PathVariable String name) {
        Course course1 = courseService.getByName(name);
        course1.setTeacher("朱柏");
        return courseService.save(course1);
    }
    @GetMapping("/delete/{name}")
    public void delete(@PathVariable String name){
        courseService.deleteByName(name);
    }
    @GetMapping("/all")
    public String findAlls() {
        PageRequest pageRequest = PageRequest.of(0, 2);
        Page<Course> page = courseService.findAll(pageRequest);
        return "总页数:" + page.getTotalPages() + "总的记录数:" + page.getTotalElements()
                + "查询结果:" + page.getContent() +
                "当前页数:" + (page.getNumber() + 1) +
                "当前记录数:" + page.getNumberOfElements() +
                "每一页记录数:" + page.getSize();
    }

}
